package com.snow.order.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author zcc 锟矫诧拷锟斤拷锟斤拷锟斤拷亩锟�
 */
public abstract class BasicDao extends BaseDao {

	private Statement stat;
	private ResultSet rs;

	protected static String TABLE_NAME;

	private void close() {
		try {

			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (stat != null) {
				stat.close();
				stat = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int insert(String[] cols, String[] values) {
		// INSERT INTO 琛�[(瀛楁1[,瀛楁2[, ...]])] VALUES (鍊�[,鍊�[, ...])

		try {
			int length;
			if (cols == null || values == null
					|| (length = cols.length) != values.length) {
				return -1;
			}
			StringBuilder sql = new StringBuilder();
			sql.append("insert into " + TABLE_NAME + " (");
			for (int i = 0; i < length; i++) {
				if (i != 0) {
					sql.append(" , ");
				}
				sql.append(cols[i]);
			}
			sql.append(" ) values (");
			for (int i = 0; i < length; i++) {
				if (i != 0) {
					sql.append(" , ");
				}
				sqlAppendValue(sql, values[i]);
			}
			sql.append(" ) ");
			this.openconnection();
			stat = conn.createStatement();
			System.out.println(sql.toString());
			return stat.executeUpdate(sql.toString());

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			
		}

		return -1;

	}

	public int delete(Map<String, String> where) {
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("delete  from " + TABLE_NAME + " where 1 = 1 ");
			Iterator<String> it = where.keySet().iterator();
			String key;
			while (it.hasNext()) {
				key = it.next();
				sql.append(" and " + key + " = ");
				sqlAppendValue(sql, where.get(key));
			}

			this.openconnection();
			stat = conn.createStatement();
			return stat.executeUpdate(sql.toString());
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			close();
		}

		return -1;
	}

	/**
	 * 
	 * @param tableName
	 * @param set
	 * @param where
	 * @return -1鐨勮瘽 鍙傛暟閿欒
	 */
	public int update(Map<String, String> set, Map<String, String> where) {
		// update t_test t set t.password = '*', t.remark = '*' where t.bs = 1;
		try {
			if (set == null) {
				return -1;
			}
			StringBuilder sql = new StringBuilder();
			sql.append("update " + TABLE_NAME + " set ");
			Iterator<String> it = set.keySet().iterator();
			String col;

			if (it.hasNext()) {
				col = it.next();
				sql.append(col + " = ");
				sqlAppendValue(sql, set.get(col));
			} else {
				return -1;
			}

			while (it.hasNext()) {
				col = it.next();
				sql.append(" ," + col + " = ");
				sqlAppendValue(sql, set.get(col));
			}

			sql.append(" where 1 = 1 ");

			it = where.keySet().iterator();
			String key;
			while (it.hasNext()) {
				key = it.next();
				sql.append(" and " + key + " = ");
				sqlAppendValue(sql, where.get(key));
			}

			this.openconnection();
			stat = conn.createStatement();
			return stat.executeUpdate(sql.toString());
		} catch (Exception e) {

			// TODO: handle exception
		} finally {
			close();
		}
		return -1;
	}

	/**
	 * no close
	 * 
	 * @param <T>
	 * 
	 * @param tableName
	 * @param cols
	 * @param where
	 * @return
	 */
	public <T> List<T> query(String[] cols, Map<String, String> where,
			IQuery query) {
		List<T> result = new ArrayList<T>();
		try {
			StringBuilder sql = new StringBuilder("select ");
			if (cols == null) {
				sql.append(" * ");
			} else {
				for (int i = 0; i < cols.length; i++) {
					if (i != 0) {
						sql.append(" , ");
					}
					sql.append(cols[i]);
				}
			}

			sql.append(" from " + TABLE_NAME + " where 1 = 1 ");
			Iterator<String> it = where.keySet().iterator();
			String key;
			while (it.hasNext()) {
				key = it.next();
				sql.append(" and " + key + " = ");
				sqlAppendValue(sql, where.get(key));
			}
			this.openconnection();
			stat = conn.createStatement();
			System.out.println(sql.toString());
			rs = stat.executeQuery(sql.toString());
			result = query.getListFromResultSet(rs);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			close();
		}
		return result;
	}

	/**
	 * 
	 * @param sql
	 * @param obj
	 *            only String and num
	 */
	private void sqlAppendValue(StringBuilder sql, Object obj) {
		if (obj instanceof String) {
			sql.append(" '" + obj.toString() + "' ");
			return;
		}
		sql.append(obj.toString());
	}

	public interface IQuery {

		/**
		 * 
		 * @param resultSet
		 * @return 瑙ｆ瀽鍚庣殑list
		 */
		public <T> List<T> getListFromResultSet(ResultSet resultSet)
				throws SQLException;

	}

}
